package com.jzo2o.customer.service.impl;

import com.jzo2o.api.publics.SmsCodeApi;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.common.enums.SmsBussinessTypeEnum;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.customer.mapper.ServerProvideMapper;
import com.jzo2o.customer.model.dto.request.InstitutionRegisterReqDTO;
import com.jzo2o.customer.model.dto.request.InstitutionResetPasswordReqDTO;
import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.customer.service.ServerProvideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class ServerProvideServiceImpl implements ServerProvideService {

    @Autowired
    private SmsCodeApi smsCodeApi;
    @Autowired
    private IServeProviderService serveProviderService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private ServerProvideMapper serverProvideMapper;

    @Override
    public void inputInfo(InstitutionRegisterReqDTO dto) {

        boolean verifyResult = smsCodeApi.verify(dto.getPhone(), SmsBussinessTypeEnum.INSTITION_REGISTER, dto.getVerifyCode()).getIsSuccess();
        if (!verifyResult) {
            throw new ForbiddenOperationException("验证码校验失败");
        }
        serveProviderService.add(dto.getPhone(), UserType.INSTITUTION, passwordEncoder.encode(dto.getPassword()));

    }

    @Override
    public void resetInfo(InstitutionResetPasswordReqDTO dto) {
        InstitutionResetPasswordReqDTO findone = serverProvideMapper.selectItemByPhone(dto.getPhone());
        if (findone == null) {
            throw new ForbiddenOperationException("该账户不存在，无法修改密码");
        }
        String password = passwordEncoder.encode(dto.getPassword());
        dto.setPassword(password);
        serverProvideMapper.resetInfo(dto.getPhone(), dto.getPassword());

    }
}
